How to comparing existing data in mysql in a more efficient way? | 您所在的位置:网站首页 › mysql use › How to comparing existing data in mysql in a more efficient way? |
You could use a unique Key to automatically decide which articles should be classed as duplicates. Take the following example: CREATE TABLE IF NOT EXISTS news_items( id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, title VARCHAR(150) NOT NULL, create_datetime DATETIME NOT NULL, description VARCHAR(255) NOT NULL, PRIMARY KEY (id), UNIQUE INDEX UK_title_create_datetime (title, create_datetime) ) ENGINE = INNODB;This table has a unique key on title and the create_datetime. The following insert statement would match items on that key and ignore them without throwing an error or inserting duplicated. INSERT IGNORE INTO news_items (title, create_datetime, description) VALUES ('My News Item', '2012-08-30 11:35:00', 'Something newsworthy happened');You could easily update the selected fields for matching rows too using ON DUPLICATE KEY UPDATE: INSERT INTO news_items (title, create_datetime, description) VALUES ('My News Item', '2012-08-30 11:35:00', 'Something newsworthy happened') ON DUPLICATE KEY UPDATE description = VALUES(description);Have a look at MySQL’s INSERT documentation and INSERT ON DUPLICATE KEY UPDATE |
CopyRight 2018-2019 实验室设备网 版权所有 |